<?php

class Checklist
{
    protected $pdo;
    protected $preProcessing;
    
    public function __construct(\PDO $pdo = null)
    {
        if ($pdo == null) {
            $dsn      = 'mysql:dbname=qas_quran; host=127.0.0.1; port=3306';
            $username = 'root';
            $password = '1234';
            $options  = array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
            ); 

            try {
                $pdo  = new \PDO($dsn, $username, $password, $options);
                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                $this->pdo = $pdo;
            } catch (\PDOException $e) {
                ?>
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">ERROR</h1>
                    </div>
                </div>
                <?php
                echo '<div class="alert alert-danger">Connection failed : ' . $e->getMessage().'</div>';
                exit;
            }
        } else {
            $this->pdo = $pdo;
        }

        $this->preProcessing = new PreProcessing($this->pdo);
    }
    
    public function searchAyat($kata)
    {
        $sql      = "SELECT * FROM `daftar_kata_quran` WHERE kata = '".strtolower($kata)."'";
        $data     = array();
        $result   = array();
        $listSura = array();
        $listAya  = array();
        $listSuraAya = array();
        $jumlahData = 0;
        foreach ($this->pdo->query($sql) as $row) {
            $data[$row['sura']][$row['aya']] = array(
                'sura' => $row['sura'],
                'aya' => $row['aya'],
            );
            $jumlahData++;
            $listSura[$row['sura']] = $row['sura'];
            $listAya[$row['aya']]   = $row['aya'];
            $listSuraAya[$row['sura']][$row['aya']] = $row['aya'];
        }

        $surat = implode("', '", $listSura);
        $ayat  = implode("', '", $listAya);
        
        $sql  = "SELECT * FROM quran_text_indonesia WHERE sura IN ('".$surat."') AND aya IN ('".$ayat."')";
        foreach ($this->pdo->query($sql) as $row) {
            if (in_array($row['sura'], $listSura)) {
                if (in_array($row['aya'], $listSuraAya[$row['sura']])) {
                    $result[$row['sura']][$row['aya']] = $row['text'];
                }
            }
        }
        
        foreach ($data as $surat => $dataSurat) {
            foreach ($dataSurat as $ayat => $value) {
                $data[$surat][$ayat]['text-indonesia'] = $result[$surat][$ayat];
            }
        }

        $namaSuraArabic = array(); 
        $namaSuraIndo = array();
        if (count($listSura) > 0) {
            $namaSurat = $this->getNamaSurat($listSura);
            $namaSuraIndo = $namaSurat['indonesia'];
            $namaSuraArabic = $namaSurat['arabic'];
        }
        $lastResult = array(
            'data' => $data,
            'jumlah-data' => $jumlahData,
            'surat-indonesia' => $namaSuraIndo,
            'surat-arabic' => $namaSuraArabic,
        );

        return $lastResult;
    }

    protected function getNamaSurat(array $listSura)
    {
        $result = array();

        $surat = implode("', '", $listSura);
        
        $sql     = "SELECT * FROM sura WHERE `index` IN ('".$surat."')";
        foreach ($this->pdo->query($sql) as $row) {
            if (in_array($row['index'], $listSura)) {
                $result['indonesia'][$row['index']] = $row['name_indonesia'];
                $result['arabic'][$row['index']] = $row['name_arabic'];
            }
        }
        
        return $result;
    }
}

